#include <tchar.h>
#include <crtdbg.h>

#include "MoveCube.h"

MoveCube::MoveCube(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
		: D3DApp(hInstance, winCaption, devType, requestedVP), totalTime(0)
{
	srand(time_t(0));

	if(!CheckDeviceCaps())
	{
		MessageBox(0, "CheckDeviceCaps FAILED!", 0, 0);
		PostQuitMessage(0);
	}

	mainCamera = new Camera((float)GetD3Dpp().BackBufferWidth,
							(float)GetD3Dpp().BackBufferHeight,
							D3DX_PI * 0.25f, 1.0f, 5000.0f);

	InitAllVertexDeclaration();

	cube = new Cube();
	grid = new Grid(100, 100, 10.0f);
	grass = new Grass();
	tree = new Tree();

	//mTexCenter = D3DXVECTOR3(216.0f, 287.5f, 0.0f);

	// Loading spritebatch
	//HR(D3DXCreateSprite(gd3dDevice, &mSprite));

	// loading texture
	//HR(D3DXCreateTextureFromFile(gd3dDevice, "image.bmp", &mTex));

	//D3DXFONT_DESC fontDesc;
	//fontDesc.Height = 80;
	//fontDesc.Width = 40;
	//fontDesc.MipLevels = 0;
	//fontDesc.Italic = true;
	//fontDesc.CharSet = DEFAULT_CHARSET;
	//fontDesc.OutputPrecision = OUT_DEFAULT_PRECIS;
	//fontDesc.Quality = DEFAULT_QUALITY;
	//fontDesc.PitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
	//_tcscpy(fontDesc.FaceName, _T("Times New Roman"));
	//D3DXCreateFontIndirect(gd3dDevice, &fontDesc, &mFont);
	
	OnResetDevice();
}

MoveCube::~MoveCube()
{
	//ReleaseCOM(mFont);
	//ReleaseCOM(mSprite);
	//ReleaseCOM(mTex);

	DestroyAllVertexDeclaration();
}

bool MoveCube::CheckDeviceCaps(){ return true; }

void MoveCube::OnLostDevice()
{
	//HR(mFont->OnLostDevice());
	//HR(mSprite->OnLostDevice());
	grid->OnLostDevice();
	cube->OnLostDevice();
	grass->OnLostDevice();
}

void MoveCube::OnResetDevice()
{
	mainCamera->OnResetDevice();
	//HR(mFont->OnResetDevice());
	//HR(mSprite->OnResetDevice());

	grid->OnResetDevice();
	cube->OnResetDevice();
	grass->OnResetDevice();

	HR(gd3dDevice->SetSamplerState(0,D3DSAMP_MAGFILTER, D3DTEXF_LINEAR));
	HR(gd3dDevice->SetSamplerState(0,D3DSAMP_MINFILTER, D3DTEXF_LINEAR));
	HR(gd3dDevice->SetSamplerState(0,D3DSAMP_MIPFILTER, D3DTEXF_LINEAR));

	HR(gd3dDevice->SetRenderState(D3DRS_LIGHTING, false));
	HR(gd3dDevice->SetRenderState(D3DRS_ALPHAREF, 10));
	HR(gd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER));
	HR(gd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true));
	HR(gd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW));

	HR(gd3dDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE));
	HR(gd3dDevice->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_SELECTARG1));
	HR(gd3dDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA));
	HR(gd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA));

	HR(gd3dDevice->SetTextureStageState(0,D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2));
}

void MoveCube::UpdateScene(float dt)
{
	gDInput->poll();

	cube->Update(dt);
	mainCamera->UpdateCamera(dt);
	totalTime += dt;
}

void MoveCube::DrawScene()
{
	HR(gd3dDevice->Clear(0, 0
		, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
		, D3DCOLOR_XRGB(255,255,255), 1.0f, 0));

	HR(gd3dDevice->BeginScene());

	D3DXMATRIX view;
	D3DXMATRIX proj;
	D3DXVECTOR3 camPos;

	camPos = mainCamera->GetPos();

	mainCamera->GetProjMtx(proj);
	mainCamera->GetViewMtx(view);

	grid->DrawGrid(view, proj, camPos, totalTime);
	cube->Draw(view, proj);
	grass->Draw(view, proj, camPos, totalTime);
	tree->Draw(view, proj);

	//mSprite->Begin( D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE);
	//HR(mSprite->Draw(mTex, 0, &mTexCenter, 0, D3DCOLOR_XRGB(255,255,255))); 

	//D3DXMATRIX T;
	//D3DXMatrixTranslation(&T, 100.0f, 2.0f, 2.0f);

	//mSprite->SetTransform(&T);
	//mSprite->Flush();
	//mSprite->End();

	//mFont->DrawText(0, _T("HELLO DIRECT 3D"), -1
	//	, &formatRect, DT_CENTER | DT_VCENTER
	//	, D3DCOLOR_XRGB(rand() % 256, rand() % 256, rand() % 256));
	
	HR(gd3dDevice->EndScene());
	HR(gd3dDevice->Present(0,0,0,0));
}